<?php include("includes/session.php"); ?>
<?php include("includes/connect_db.php"); ?>
<?php include("includes/header.php"); ?>
<?php include("notif.php"); ?>

<style>	
	.mandatory {
		color: red;
		margin-right: 5px;
	}
</style>

<div class="span9">  
  <h3>SMS</h3>`    
  <div class="alert alert-info">Tambah User</div>
    
	<form class="form-horizontal" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
  	<div>
			<div class="span12">
				<div class="span8">
					<div class="control-group">
						<label class="control-label"><span class="mandatory">*</span>NPP</label>
						<div class="controls">
							<input id="kode_user" class="required" placeholder="Masukkan NPP" type="text" name="kode_user" value="">
						</div>
					</div>

					<div class="control-group">
						<label class="control-label"><span class="mandatory">*</span>Nama</label>
						<div class="controls">
							<input id="nama" class="required" placeholder="Masukkan Nama" type="text" name="nama" value="">
						</div>
					</div>
					
					<div class="control-group">
						<label class="control-label">Tanggal Lahir</label>
						<div class="controls">
							<input id="tanggal" placeholder="Masukkan Tanggal Lahir" type="date" name="tanggal" value="" class="datePicker">
						</div>
					</div>

					<div class="control-group">
						<label class="control-label"><span class="mandatory">*</span>Email</label>
						<div class="controls">
							<input id="email" class="required" placeholder="Masukkan Email" type="email" name="email" value="">
						</div>
					</div>

					<div class="control-group">
						<label class="control-label"><span class="mandatory">*</span>Username</label>
						<div class="controls">
							<input id="username" class="required" placeholder="Masukkan Username" type="text" name="username" value="">
						</div>
					</div>

					<div class="control-group">
						<label class="control-label"><span class="mandatory">*</span>Password</label>
						<div class="controls">
							<input id="password" class="required" placeholder="Masukkan Password" type="password" name="password" value="">
						</div>
					</div>

					<div class="control-group">
						<label class="control-label"><span class="mandatory">*</span>Konfirmasi Password</label>
						<div class="controls">
							<input id="konfirmasi_password" class="required" placeholder="Masukkan Password" type="password" name="konfirmasi_password" value="">
						</div>
					</div>
				</div>
			</div>
			<div>
				<div class="span12">
					<ul class="breadcrumb well">
						<button id="simpan" class="btn btn-info" name="simpan" disabled="disabled"><i class="icon-white icon-ok"></i> Simpan</button>
						<a href="/smsgateway" id="batal" class="btn btn-danger"><i class="icon-white icon-remove"></i> Batal</a>
					</ul>
				</div>
			</div>
		</div>
	</form>
</div>

<?php include("includes/footer.php"); ?>

<?php
	if(isset($_POST['simpan'])){
		$kode_user = $_POST['kode_user'];
		$nama = $_POST['nama'];
		$tanggal = $_POST['tanggal'];
		$email = $_POST['email'];
		$username = $_POST['username'];
		$password = md5($_POST['password']);

		echo "<SCRIPT> alert('Data Telah Berhasil Disimpan'); window.location.href='user.php'</SCRIPT>";

		$sql_insert_user = "INSERT INTO tb_user (kode_user, nama, tgl_lahir, email, username, password) VALUES ('$kode_user', '$nama', '$tanggal', '$email', '$username', '$password')";
		if (mysql_query($sql_insert_user)) {
			echo "<SCRIPT> alert('Data Telah Berhasil Disimpan'); window.location.href='user.php'</SCRIPT>";
		} else {
			die(mysql_error());
		}
	}
?>

<script type="text/javascript">
	function check_empty_form() {
		enable = true;
		$('.required').each(function(index, el) {
			if ($(this).val() == "") {
				enable = false
			};
		});
		if (enable) {
			$('#simpan').removeAttr('disabled');
		} else {
			$('#simpan').attr('disabled', 'disabled');
		}
	}

	function enable_button() {
		$('#simpan').removeAttr('disabled');
	}

	function disable_button() {
		$('#simpan').attr('disabled', 'disabled');
	}

	function check_duplicate(var_data, var_aksi) {
		var_send = {
			aksi: var_aksi,
			data_input: var_data.val()
		}

		$.ajax({
			url: 'act_check_user.php',
			type: 'POST',
			data: var_send,
			cache: false, 
      success: function(msg){ 
      	if (msg != "valid") {
        	var_data.closest('.control-group').addClass('error');
      	}
      } 
		})
	}

	$(document).ready(function() {
		$('.required').live('keydown', function(event) {
			check_empty_form();
		});

		$('#konfirmasi_password').keyup(function(event) {
			if ($(this).val() != $('#password').val()) {
				$(this).closest('.control-group').addClass('error');
				disable_button();
			} else {
				$(this).closest('.control-group').removeClass('error');
				enable_button();
			};
		});

		$('#username').keyup(function(event) {
			var that = $(this)
			check_duplicate(that, '1');
		});

		$('#kode_user').keyup(function(event) {
			var that = $(this)
			check_duplicate(that, '2');
		});

		$('#email').keyup(function(event) {
			var that = $(this)
			check_duplicate(that, '3');
		});
	});
</script>